<?php
namespace Modules\Admin\Model\Data;

use Modules\Admin\Model\Dao\Db\Menu\Menu as DaoDbMenuMenu;
use Modules\Admin\Model\Dao\Db\Menu\Controller as DaoDbMenuController;

/**
 * 菜单管理
 */
class Menu {

    /**
     * 获取所有菜单控制器信息
     * @return array
     */
    public function getMenuControlList(){
        $menu_list = (new DaoDbMenuController())->getList();
        return $menu_list;
    }

    /**
     * 获取菜单控制数据列表
     * @return array
     */
    public function getMenuControlAll() {
        $menu_list = $this->getMenuControlList();
        $menus = array();
        foreach ($menu_list as $value) {
            $menus[$value['mid']][] = $value;
        }
        return $menus;
    }

    /**
     * 获取所有菜单信息
     * @return array
     */
    public function getMenuList(){
        $menu_list = (new DaoDbMenuMenu())->getList();
        return $menu_list;
    }

    /**
     * 获取所有菜单id和名字用于左边菜单项
     * @return array
     */
    public function getLeftNav() {
        $menu_list = $this->getMenuList();
        $nav = array();
        foreach ($menu_list as $menu) {
            $nav[$menu['mid']] = $menu['mname'];
        }
        return $nav;
    }

    /**
     * 获取一条菜单控制器信息
     * @param integer $id 菜单id
     * @return array
     */
    public function getMenuControlOneById($id){
        $menu_control_info = (new DaoDbMenuController())->getInfoById($id);
        return $menu_control_info;
    }

    /**
     * 根据id取菜单信息
     * @param $id
     * @return array
     */
    public function getMenuOneById($id){
        $menu_info = (new DaoDbMenuMenu())->getInfoById($id);
        return $menu_info;
    }

    /**
     * 保存菜单信息
     * @param      $info
     * @param null $id
     * @return bool
     */
    public function saveMenuInfo($info, $id = null){
        $ret = (new DaoDbMenuMenu())->save($info, $id);
        return $ret;
    }

    /**
     * 保存菜单控制器信息
     * @param array        $info 菜单数组
     * @param integer|null $id 菜单id
     * @return boolean
     */
    public function saveMenuControlInfo($info, $id = null) {
        $ret = (new DaoDbMenuController())->save($info, $id);
        return $ret;
    }

    /**
     * 删除菜单及子菜单信息
     * @param $id
     * @return bool
     */
    public function delMenuOneById($id){
        $ret1 = (new DaoDbMenuMenu())->delById($id);
        $ret2 = (new DaoDbMenuController())->delByMid($id);
        return $ret1 && $ret2;
    }

    /**
     * 删除子菜单信息(控制器)
     * @param integer $id 菜单id
     * @return boolean
     */
    public function delMenuControlOneById($id){
        $ret = (new DaoDbMenuController())->delById($id);
        return $ret;
    }
}
